package setting

import (
	"errors"

	"github.com/spf13/viper"

	"shorten/pkg/file"
)

type Setting struct {
	vp *viper.Viper
}

// NewSetting 实例化Setting
func NewSetting(path string) (*Setting, error) {
	if path == "" {
		path = "./conf/config.yaml"
	}
	if file.CheckNotExist(path) == true {
		return nil, errors.New("配置文件不存在")
	}
	vp := viper.New()
	vp.SetConfigFile(path)
	err := vp.ReadInConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}

// ReadConf 读取配置
func (s *Setting) ReadConf(v interface{}) error {
	return s.vp.Unmarshal(&v)
}
